package org.pedia2.apps.demo.helloworld.server;

import java.util.List;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;

import org.pedia2.apps.demo.helloworld.client.GreetingService;
import org.pedia2.apps.demo.helloworld.client.model.City;
import org.pedia2.apps.demo.helloworld.client.model.Taxi;
import org.pedia2.apps.demo.helloworld.server.jdo.PMF;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;
/**
 * The server side implementation of the RPC service.
 */
@SuppressWarnings("serial")
public class GreetingServiceImpl extends RemoteServiceServlet implements GreetingService{

	public String greetServer(String input) {
		String serverInfo = getServletContext().getServerInfo();
		String userAgent = getThreadLocalRequest().getHeader("User-Agent");
		return "Hello, " + input + "!<br><br>I am running " + serverInfo + ".<br><br>It looks like you are using:<br>"
				+ userAgent;
	}

	public City createCity(String name) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		try {
			City newCity = new City();
			newCity.setName(name);
			pm.makePersistent(newCity);
			//
			newCity = pm.detachCopy(newCity);
			return newCity;
		} finally {
			pm.close();
		}
	}

	@SuppressWarnings("unchecked")
	public List<City> getCityList() {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Query q = pm.newQuery(City.class);
		List<City> returnList = (List<City>) q.execute();
		returnList = (List<City>) pm.detachCopyAll(returnList);
		return returnList;
	}

	public Taxi createTaxi(String taxiCompanyName, String taxiCompanyPhone, String cityId) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		City city = pm.getObjectById(City.class, cityId);
		System.out.println(city.getName());
		if (city == null)
			throw new NullPointerException("City not found!");
		try {
			Taxi newTaxi = new Taxi();
			newTaxi.setName(taxiCompanyName);
			newTaxi.setPhone(taxiCompanyPhone);
			newTaxi.setCity(city);
			city.addTaxiToTaxiList(newTaxi);
			pm.makePersistent(newTaxi);
			//
			newTaxi = pm.detachCopy(newTaxi);
			return newTaxi;
		} finally {
			pm.close();
		}
	}

	@SuppressWarnings("unchecked")
	public List<Taxi> getAllTaxiList() {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Query q = pm.newQuery(Taxi.class);
		List<Taxi> returnList = (List<Taxi>) q.execute();
		returnList = (List<Taxi>) pm.detachCopyAll(returnList);
		return returnList;
	}

	public List<Taxi> getTaxiListForCity(String cityId) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		try {
			City city = pm.getObjectById(City.class, cityId);
			List<Taxi> taxiList = city.getTaxiList();
			taxiList = (List<Taxi>) pm.detachCopyAll(taxiList);
			return taxiList;
		} finally {
			pm.close();
		}
	}

	public Taxi updateTaxi(String taxiId, String taxiCompanyName, String taxiCompanyPhone) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		try {
			Taxi taxi = pm.getObjectById(Taxi.class, taxiId);
			taxi.setName(taxiCompanyName);
			taxi.setPhone(taxiCompanyPhone);
			pm.makePersistent(taxi);
			taxi.getCity();
			taxi = pm.detachCopy(taxi);
			return taxi;
		} finally {
			pm.close();
		}
	}

	public Taxi changeCityOfTaxi(String taxiId, String cityId) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		try {
			Taxi taxi = pm.getObjectById(Taxi.class, taxiId);
			Taxi newTaxi = new Taxi();
			newTaxi.setName(taxi.getName());
			newTaxi.setPhone(taxi.getPhone());
			City newCity = pm.getObjectById(City.class, cityId);
			newTaxi.setCity(newCity);
			newCity.addTaxiToTaxiList(newTaxi);
			pm.deletePersistent(taxi);
			pm.makePersistent(newTaxi);
			/*	pm.makePersistent(newCity);
				pm.makePersistent(oldCity);*/
			newTaxi = pm.detachCopy(newTaxi);
			return newTaxi;
		} finally {
			pm.close();
		}
	}

	public Taxi getTaxi(String taxiId) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		try {
			Taxi taxi = pm.getObjectById(Taxi.class, taxiId);
			taxi.getCity();
			taxi = pm.detachCopy(taxi);
			return taxi;
		} finally {
			pm.close();
		}
	}

	public City getCity(String cityId) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		try {
			City city = pm.getObjectById(City.class, cityId);
			city = pm.detachCopy(city);
			return city;
		} finally {
			pm.close();
		}
	}
}
